package com.feicheng.controller.main.admin.client;


import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Client;
import com.feicheng.service.IFClientService;
import com.feicheng.service.impl.FClientServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/main/admin/client")
@RequestMapping("/main/admin/client")
public class ClientController {

    @Autowired
    private IFClientService fClientService;

    @GetMapping("/page")
    public ResultInfo getClientByPage(
            String clientId,
            String clientName,
            String phone,
            Boolean sex,
            Integer age,
            Integer maxAge,
            Integer minAge,
            Integer storeId,
            String name,
            Integer depositAmount,
            Integer vipTimeLongs,
            Boolean dFlag,
            Boolean vipStatu,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        JSONObject result = new JSONObject();
        result.put(Constant.ENTITY.CLIENT.CLIENT_ID, clientId);
        result.put(Constant.ENTITY.CLIENT.CLIENT_NAME, clientName);
        result.put(Constant.ENTITY.CLIENT.PHONE, phone);
        result.put(Constant.ENTITY.CLIENT.SEX, sex);
        result.put(Constant.ENTITY.CLIENT.AGE, age);
        result.put(Constant.HTTP.PARAM.MAXAGE,maxAge);
        result.put(Constant.HTTP.PARAM.MINAGE,minAge);
        result.put(Constant.ENTITY.STORE.NAME, name);
        result.put(Constant.ENTITY.STORE.STORE_ID, storeId);
        result.put(Constant.ENTITY.DEPOSIT_RECORD.AMOUNT, depositAmount);
        result.put(Constant.ENTITY.ACCOUNT.TIME_LONGS, vipTimeLongs);
        result.put(Constant.ENTITY.CLIENT.D_FLG,dFlag);
        result.put(Constant.ENTITY.VIP.STATU, vipStatu);
        result.put(Constant.HTTP.PARAM.CURRENT, current);
        result.put(Constant.HTTP.PARAM.SIZE, size);
        fClientService.getClientsByPage(result);

        return fClientService.getClientsByPage(result);
    }

    @PutMapping("/{clientId}/freezen")
    public ResultInfo freezenClient(@PathVariable(name="clientId") Integer clientId){
        return this.delClient(clientId);
    }

    @PutMapping("/{clientId}/activate")
    public ResultInfo activateClient(@PathVariable(name="clientId") Integer clientId){
        Client client = new Client();
        client.setClientId(clientId);
        client.setDFlg(false);

        return this.updateClient(client);
    }

    @DeleteMapping("/info")
    public ResultInfo delClient(Integer clientId){
        if (null == clientId) {
            // TODO 客户ID缺失
            throw new SystemException("客户ID缺失");
        }
        return fClientService.deleteClient(clientId);
    }

    @PutMapping("/info")
    public ResultInfo updateClient(Client client) {
        if (null == client.getClientId()) {
            // TODO 客户ID缺失
            throw new SystemException("客户ID缺失");
        }
        return fClientService.updateClient(client);
    }

    @GetMapping("/info")
    public ResultInfo selectById(Integer clientId){
         return fClientService.getClient(clientId);
    }

    @GetMapping("/codeInfo")
    public ResultInfo getQode(Integer clientId, Integer storeId){
        return fClientService.getQcode(clientId, storeId);
    }




}
